var url = require('url');
var queryString = require('querystring');
var models = require('./models');
var async = require('async');

function NewBudget(request, response){
	var name = request.body.name;
	var owner = request.session.user;
	if(!owner){
		console.log('Not logged in, saving budget to nate');
		owner = 'nate';
	}
	
	var budget = new models.budget();
	budget.owner = owner;
	budget.name = name;
	budget.save();
	response.send({id: budget._id, doc: budget});
}

function GetData(request, response){
	var query = queryString.parse(url.parse(request.url).query);
	var model_name = query.model_name;
	console.log('Request by: '+request.session.user);
	console.log('Data requested: '+model_name);
	if(model_name == 'budget'){
		models.budget.find({'owner':request.session.user}, function(err, docs){
			if(docs.length==0){
				console.log('No budgets found');
			}
			response.send(docs);
		});
		return;
	}
	else{
		var budget = query.budget;
		console.log('Request for '+model_name);
		models[model_name].find({'owner':request.session.user, budget: budget}, function(err, docs){
			if(!docs){
				docs = {};
			}
			response.send(docs);
			return;
		});
	}
}

function RequestNewInstance(request, response){
	var query = queryString.parse(url.parse(request.url).query);
	var type = query.type;
	var model = models[type];
	console.log('New instance of '+type+' requested.');
	var instance = new model();
	instance.owner = request.session.user;
	response.send(instance);
}

// For saving income and expenses
function SaveData(request, response){
	var model = models[request.body.model_type];
	if(!model){
		console.log('Model type \''+request.body.model_type+'\' not found');
		response.send({success: false});
		return;
	}
	
	var budget_id = request.body.budget_id;
	var data = JSON.parse(request.body.data)
	
	// Delete all the old ones
	model.find({budget: budget_id}, function(err, docs){
		for(var key in docs){
			docs[key].remove();
		}
	});
	
	// Save all the new ones
	for(var key in data){
		var item = data[key];
		if(typeof item == 'function'){
			continue;
		}
		var doc = new model();
		for(var key in item){
			doc[key] = item[key];
		}
		doc.budget = budget_id;
		doc.save();
	}
	response.send({success: true})
}	
	
function SaveInstance(request, response){ 
	var data = request.body.data;
	var model_type = request.body.type;
	models[model_type].findById(data._id, function(err, doc){
		if(err){
			console.log(err);
			response.send({success: false});
			return;
		}
		if(!doc){
			doc = new models[model_type]();
			doc.owner = request.session.user;
		}
		for(var key in data){
			var value = data[key];
			doc[key] = value;
		}
		console.log('saving: '+doc);
		doc.save();
		response.send({success: true});
	});
}	

function DeleteInstance(request, response){
	var id = request.body.id;
	var type = request.body.type;
	console.log('Deleting '+id+' of type '+type+'.');
	models[type].findOne({_id: id}, function(err, doc){
		if(doc){
			doc.remove();
		}
	});
	response.send({success: true});
}

exports['new_budget'] = NewBudget;

exports['save_data'] = SaveData;
exports['get_data'] = GetData;
exports['request_new_instance'] = RequestNewInstance;
exports['save_instance'] = SaveInstance;
exports['delete_instance'] = DeleteInstance;